<?php

class IO_Page
{
	private $intNumberPagePerNavi = 5;/// so trang tren 1 phan trang
	private $intNumberRecordPerPage = 20;//so record ket qua tren 1 trang
	private $strFunctionOnclick = 'onPage';// ten function de goi javasctip khi click tren cac trang
	
	function IO_Page($intNumberPagePerNavi = 5, $intNumberRecordPerPage = 2, $strFunctionOnclick = 'onPage')
	{
		$this -> intNumberPagePerNavi = $intNumberPagePerNavi;
		$this -> intNumberRecordPerPage = $intNumberRecordPerPage;
		$this -> strFunctionOnclick = $strFunctionOnclick;
	}
		
	/*
	* Hàm tao phan trang
	* Type: Function <br />
	*	Name: createPageNavigator <br />
	*	Modify: Thach Le <thach.le@ionetwork.com> <br />
	*	Create date: Oct 12 08 <br />
	* Descript:
	* Param: 	$startPage: trang hien tai, 
	*					$total: Tong so records,
	*					
	*/		
	public function createPageNavigator($startPage, $total, $attach = '')
	{	
		/// tinh so trang
		$strPage = "";
		$numberPage = ceil($total / $this->intNumberRecordPerPage);
		if ( $numberPage == 1 )
			return $strPage;
				
		// xac dinh trang dau, trang cuoi
		$start = $startPage;// truoc tien gan trang dau = trang hien tai
				
		if ( $start - ceil($this->intNumberPagePerNavi/2) <= 0 )
		{
			$start = 1;
		}
		else
		{
			if ( ($numberPage - $start) >= floor($this->intNumberPagePerNavi/2) )
				$start = $start - floor($this->intNumberPagePerNavi/2);
			else
			{
				$start = $numberPage - $this->intNumberPagePerNavi + 1;
				if ( $start <= 0 )
					$start = 1;
			}
		}
		
		if ( $numberPage >= $start + $this->intNumberPagePerNavi )
			$end = $start + $this->intNumberPagePerNavi - 1;
		else
			$end = $numberPage;
					
		for ( $i = $start; $i <= $end; $i++ )
		{
			if ( $i == $startPage )
			{
				$strHref = '<td><a class="link_phantrang active_p">'. $i . '</a></td><td width="5"></td>' ;
			}
			else
			{
				$strHref = "<td> <a href='#' title='Trang ".$i."' class='link_phantrang' onclick='javascript:" . $this->strFunctionOnclick . "(" . $i . ",\"" . $attach . "\");return false;'>";
				$strHref .= $i;
				$strHref .= '</a></td><td width="5"></td>';
			}
			
			$strPage .=  $strHref;
			if ( $i != $end )
				$strPage .= "&nbsp;";
		}
		
		if ( $startPage > 1 ) 
		{	
			$tmp = "<td><a href='#' title='Trang trước' class='link_phantrang_fn' onclick='javascript:" . $this->strFunctionOnclick . "(1,\"" . $attach . "\");return false;'>";
			$tmp .= "&lt;&lt;</a></td><td width='10'></td>";
			$tmp .= "<td><a href='#' title='Trang trước' class='link_phantrang_fn' onclick='javascript:" . $this->strFunctionOnclick . "(" . ($startPage-1) . ",\"" . $attach . "\");return false;'>";
			$tmp .= '&lt;';
			$tmp .= "</a></td>";
			$tmp .= '<td width="5"></td>';
			$strPage = $tmp . $strPage;
		}
		
		if ( $startPage < $end )
		{
			$strPage .= '';
			$strPage .= "<td><a href='#' title='Trang sau' class='link_phantrang_fn' onclick='javascript:" . $this->strFunctionOnclick . "(" . ($startPage+1) . ",\"" . $attach . "\");return false;'>";
			$strPage .= "&gt;";
			$strPage .= "</a></td>";
			
			$strPage .= '<td width="10"></td>';
			$strPage .= "<td><a href='#' title='Trang sau' class='link_phantrang_fn' onclick='javascript:" . $this->strFunctionOnclick . "(" . ($numberPage) . ",\"" . $attach . "\");return false;'>";
			$strPage .= "&gt;&gt;";
			$strPage .= "</a></td>";
		}
		return $strPage;		
	}
}

?>